package cn.layfolk.order.controller;

import cn.hutool.json.JSONObject;
import cn.layfolk.order.model.UserDTO;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
public class OrderController {

    @GetMapping(value = "/r1")
    public String r1() {
        String user = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user + "访问资源1";
    }


    @GetMapping(value = "/r2")
    public String r2() {

        //从Header中获取用户信息
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //String userStr = request.getHeader("Authorization");
        String userStr = request.getHeader("user");
        JSONObject userJsonObject = new JSONObject(userStr);
        UserDTO userDTO = new UserDTO();
        //userDTO.setUsername(userJsonObject.getStr("user_name"));
        //userDTO.setId(Long.parseLong(userJsonObject.get("id").toString()));


        return "访问资源2";
    }


}